﻿package org.hyy.base;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnection {
	private final String url = "jdbc:mysql://localhost/shopping?user=root&password=root";
	
	/**
	 * 获得数据库连接对象
	 * @return
	 */
	public Connection getCon(){
		//加载驱动程序
		try {
			Class.forName("com.mysql.jdbc.Driver");
						   
			//获得数据库连接
			try {
				return DriverManager.getConnection(url);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 关闭数据库连接对象
	 * @param con
	 */
	public void close(Connection con){
		if(con != null){
			try {
				con.close();
				con = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
	}
}
